package quests.Q402_PathToAHumanKnight;

import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;
import net.sf.l2j.gameserver.model.quest.State;
import net.sf.l2j.gameserver.network.serverpackets.SocialAction;

/**
 * @author alexa026
 */

public class Q402_PathToAHumanKnight extends Quest
{
	private static final String qn = "Q402_PathToAHumanKnight";
	
	// Reward
	private final static int SwordOfRitual = 1161;
		
	//items
	private final static int MarkOfEsquire = 1271;
	private final static int CoinOfLords1 = 1162;
	private final static int CoinOfLords2 = 1163;
	private final static int CoinOfLords3 = 1164;
	private final static int CoinOfLords4 = 1165;
	private final static int CoinOfLords5 = 1166;
	private final static int CoinOfLords6 = 1167;
	private final static int GludioGuardsMark1 = 1168;
	private final static int BugbearNecklace = 1169;
	private final static int EinhasadChurchMark1 = 1170;
	private final static int EinhasadCrucifix = 1171;
	private final static int GludioGuardsMark2 = 1172;
	private final static int SpiderLeg = 1173;
	private final static int EinhasadChurchMark2 = 1174;
	private final static int LizardmanTotem = 1175;
	private final static int GludioGuardsMark3 = 1176;
	private final static int GiantSpiderHusk = 1177;
	private final static int EinhasadChurchMark3 = 1178;
	private final static int HorribleSkull = 1179;
		
	//NPCs
	private final static int SirKlaus = 30417;
	private final static int CaptainBathis = 30332;
	private final static int Raymond = 30289;
	private final static int CaptainBazique = 30379;
	private final static int Levian = 30037;
	private final static int CaptainGilbert = 30039;
	private final static int Biotin = 30031;
	private final static int SirAaronTanford = 30653;
	private final static int SirCollinWindawood = 30311;

		
	public Q402_PathToAHumanKnight(int questId, String name, String descr)
	{
		super(questId, name, descr);
	
		questItemIds = new int[] { MarkOfEsquire, CoinOfLords1, CoinOfLords2, CoinOfLords3, CoinOfLords4, CoinOfLords5, CoinOfLords6, GludioGuardsMark1, BugbearNecklace, EinhasadChurchMark1, EinhasadCrucifix, GludioGuardsMark2, SpiderLeg, EinhasadChurchMark2, LizardmanTotem, GludioGuardsMark3, GiantSpiderHusk, EinhasadChurchMark3, LizardmanTotem, GludioGuardsMark3, GiantSpiderHusk, EinhasadChurchMark3, HorribleSkull };
			
		addStartNpc(SirKlaus);
		addTalkId(SirKlaus, CaptainBathis, Raymond, CaptainBazique, Levian, CaptainGilbert, Biotin, SirAaronTanford, SirCollinWindawood);
			
		addKillId(20775, 27024, 20038, 20043, 20050, 20030, 20027, 20024, 20103, 20106, 20108, 20404);
			
	}

	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (event.equalsIgnoreCase("30417-01.htm"))
		{
			if (player.getClassId().getId() == 0x00)
			{
				if (player.getLevel() >= 19)
				{
					if (st.getQuestItemsCount(SwordOfRitual) == 1)
						htmltext = "30417-04.htm";
					else
						htmltext = "30417-05.htm";
				}
				else
					htmltext = "30417-02.htm";
			}
			else if (player.getClassId().getId() == 0x07)
				htmltext = "30417-02b.htm";
			else
				htmltext = "30417-02.htm";
		}
		
		else if (event.equalsIgnoreCase("30417-08.htm"))
		{
			st.set("cond", "1");
			st.setState(State.STARTED);
			st.playSound("ItemSound.quest_accept");
			st.giveItems(MarkOfEsquire, 1);
		}
		
		else if (event.equalsIgnoreCase("30332-02.htm"))
		{
			st.giveItems(GludioGuardsMark1, 1);
			st.playSound("ItemSound.quest_middle");
		}
		
		else if (event.equalsIgnoreCase("30289-03.htm"))
		{
			st.giveItems(EinhasadChurchMark1, 1);
			st.playSound("ItemSound.quest_middle");
		}
		
		else if (event.equalsIgnoreCase("30379-02.htm"))
		{
			st.giveItems(GludioGuardsMark2, 1);
			st.playSound("ItemSound.quest_middle");
		}
		
		else if (event.equalsIgnoreCase("30037-02.htm"))
		{
			st.giveItems(EinhasadChurchMark2, 1);
			st.playSound("ItemSound.quest_middle");
		}
		
		else if (event.equalsIgnoreCase("30039-02.htm"))
		{
			st.giveItems(GludioGuardsMark3, 1);
			st.playSound("ItemSound.quest_middle");
		}
		
		else if (event.equalsIgnoreCase("30031-02.htm"))
		{
			st.giveItems(EinhasadChurchMark3, 1);
			st.playSound("ItemSound.quest_middle");
		}
		
		else if (event.equalsIgnoreCase("30417-13.htm"))
		{
			st.takeItems(CoinOfLords1, 1);
			st.takeItems(CoinOfLords2, 1);
			st.takeItems(CoinOfLords3, 1);
			if (st.getQuestItemsCount(CoinOfLords4) < 1)
				st.takeItems(CoinOfLords4, 1);
			else if (st.getQuestItemsCount(CoinOfLords5) < 1)
				st.takeItems(CoinOfLords5, 1);
			else if (st.getQuestItemsCount(CoinOfLords6) < 1)
				st.takeItems(CoinOfLords6, 1);
			st.takeItems(MarkOfEsquire, 1);
			st.giveItems(SwordOfRitual, 1);
			st.addExpAndSp(3200, 1500);
			player.broadcastPacket(new SocialAction(player, 3));

			st.playSound("ItemSound.quest_finish");
			st.exitQuest(false);
			st.saveGlobalQuestVar("1ClassQuestFinished", "1");
		}
		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = getNoQuestMsg(player);
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		int coins = ((st.getQuestItemsCount(CoinOfLords1) + st.getQuestItemsCount(CoinOfLords2) + st.getQuestItemsCount(CoinOfLords3) + st.getQuestItemsCount(CoinOfLords4) + st.getQuestItemsCount(CoinOfLords5) + st.getQuestItemsCount(CoinOfLords6)));
		int cond = st.getInt("cond");
		switch(st.getState())
		{
			case State.CREATED:
				htmltext = "30417-01.htm";
				break;
			case State.STARTED:
				switch(npc.getNpcId())
				{
					case SirKlaus:
						if (cond == 1)
						{
							if (coins < 3)
								htmltext = "30417-09.htm";
							else if (coins >= 3)
								htmltext = "30417-10.htm";
							else if (coins == 6)
								htmltext = "30417-13.htm";
						}
						break;
					case Levian:
						if (st.getQuestItemsCount(EinhasadChurchMark2) == 0 && st.getQuestItemsCount(CoinOfLords4) == 0)
							htmltext = "30037-01.htm";
						else if (st.getQuestItemsCount(EinhasadChurchMark2) == 1)
						{
							if (st.getQuestItemsCount(LizardmanTotem) < 20)
								htmltext = "30037-03.htm";
							else
							{
								htmltext = "30037-04.htm";
								st.takeItems(LizardmanTotem, -1);
								st.takeItems(EinhasadChurchMark2, 1);
								st.giveItems(CoinOfLords4, 1);
								st.playSound("ItemSound.quest_middle");
							}
						}
						else if (st.getQuestItemsCount(CoinOfLords4) == 1)
							htmltext = "30037-05.htm";
						break;
					case CaptainBazique:
						if (st.getQuestItemsCount(GludioGuardsMark2) == 0 && st.getQuestItemsCount(CoinOfLords3) == 0)
							htmltext = "30379-01.htm";
						else if (st.getQuestItemsCount(GludioGuardsMark2) == 1)
						{
							if (st.getQuestItemsCount(SpiderLeg) < 20)
								htmltext = "30379-03.htm";
							else
							{
								htmltext = "30379-04.htm";
								st.takeItems(SpiderLeg, -1);
								st.takeItems(GludioGuardsMark2, 1);
								st.giveItems(CoinOfLords3, 1);
								st.playSound("ItemSound.quest_middle");
							}
						}
						else if (st.getQuestItemsCount(CoinOfLords3) == 1)
							htmltext = "30379-05.htm";
						break;
					case Raymond:
						if (st.getQuestItemsCount(EinhasadChurchMark1) == 0 && st.getQuestItemsCount(CoinOfLords2) == 0)
							htmltext = "30289-01.htm";
						else if (st.getQuestItemsCount(EinhasadChurchMark1) == 1)
						{
							if (st.getQuestItemsCount(EinhasadCrucifix) < 12)
								htmltext = "30289-04.htm";
							else
							{
								htmltext = "30289-05.htm";
								st.takeItems(EinhasadCrucifix, -1);
								st.takeItems(EinhasadChurchMark1, 1);
								st.giveItems(CoinOfLords2, 1);
								st.playSound("ItemSound.quest_middle");
							}
						}
						else if (st.getQuestItemsCount(CoinOfLords2) == 1)
							htmltext = "30289-06.htm";
						break;
					case CaptainBathis:
						if (st.getQuestItemsCount(GludioGuardsMark1) == 0 && st.getQuestItemsCount(CoinOfLords1) == 0)
							htmltext = "30332-01.htm";
						else if (st.getQuestItemsCount(GludioGuardsMark1) == 1)
						{
							if (st.getQuestItemsCount(BugbearNecklace) < 10)
								htmltext = "30332-03.htm";
							else
							{
								htmltext = "30332-04.htm";
								st.takeItems(BugbearNecklace, -1);
								st.takeItems(GludioGuardsMark1, 1);
								st.giveItems(CoinOfLords1, 1);
								st.playSound("ItemSound.quest_middle");
							}
						}
						else if (st.getQuestItemsCount(CoinOfLords1) == 1)
							htmltext = "30332-05.htm";
						break;
					case Biotin:
						if (st.getQuestItemsCount(EinhasadChurchMark3) == 0 && st.getQuestItemsCount(CoinOfLords6) == 0)
							htmltext = "30031-01.htm";
						else if (st.getQuestItemsCount(EinhasadChurchMark3) == 1)
						{
							if (st.getQuestItemsCount(HorribleSkull) < 10)
								htmltext = "30031-03.htm";
							else
							{
								htmltext = "30031-04.htm";
								st.takeItems(HorribleSkull, -1);
								st.takeItems(EinhasadChurchMark3, 1);
								st.giveItems(CoinOfLords6, 1);
								st.playSound("ItemSound.quest_middle");
							}
						}
						else if (st.getQuestItemsCount(CoinOfLords6) == 1)
							htmltext = "30031-05.htm";
						break;
					case CaptainGilbert:
						if (st.getQuestItemsCount(GludioGuardsMark3) == 0 && st.getQuestItemsCount(CoinOfLords5) == 0)
							htmltext = "30039-01.htm";
						else if (st.getQuestItemsCount(GludioGuardsMark3) == 1)
						{
							if (st.getQuestItemsCount(GiantSpiderHusk) < 20)
								htmltext = "30039-03.htm";
							else
							{
								htmltext = "30039-04.htm";
								st.takeItems(GiantSpiderHusk, -1);
								st.takeItems(GludioGuardsMark3, 1);
								st.giveItems(CoinOfLords5, 1);
								st.playSound("ItemSound.quest_middle");
							}
						}
						else if (st.getQuestItemsCount(CoinOfLords5) == 1)
							htmltext = "30039-05.htm";
						break;
				}
		}
	
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return null;
		
		int npcId = npc.getNpcId();
		
		if (st.getQuestItemsCount(GludioGuardsMark1) == 1 && npcId == 20775)
		{
			if (st.getQuestItemsCount(BugbearNecklace) < 10)
				st.giveItems(BugbearNecklace, 1);
			else if (st.getQuestItemsCount(BugbearNecklace) == 10)
				st.playSound("ItemSound.quest_middle");
		}
		
		else if (st.getQuestItemsCount(EinhasadChurchMark1) == 1 && npcId == 27024)
		{
			if (st.getQuestItemsCount(EinhasadCrucifix) < 12)
				st.giveItems(EinhasadCrucifix, 1);
			else if (st.getQuestItemsCount(EinhasadCrucifix) == 12)
				st.playSound("ItemSound.quest_middle");
		} 
		
		else if (st.getQuestItemsCount(GludioGuardsMark2) == 1 && (npcId == 20038 || npcId == 20043 || npcId == 20050))
		{
			if (st.getQuestItemsCount(SpiderLeg) < 20)
				st.giveItems(SpiderLeg, 1);
			else if (st.getQuestItemsCount(SpiderLeg) == 20)
				st.playSound("ItemSound.quest_middle");
		}
		
		else if (st.getQuestItemsCount(EinhasadChurchMark2) == 1 && (npcId == 20030 || npcId == 20027 || npcId == 20024))
		{
			if (st.getQuestItemsCount(LizardmanTotem) < 20)
				st.giveItems(LizardmanTotem, 1);
			else if (st.getQuestItemsCount(LizardmanTotem) == 20)
				st.playSound("ItemSound.quest_middle");
		}
		
		else if (st.getQuestItemsCount(GludioGuardsMark3) == 1 && (npcId == 20103 || npcId == 20106 || npcId == 20108))
		{
			if (st.getQuestItemsCount(GiantSpiderHusk) < 20)
				st.giveItems(GiantSpiderHusk, 1);
			else if (st.getQuestItemsCount(GiantSpiderHusk) == 20)
				st.playSound("ItemSound.quest_middle");
		}
		
		else if (st.getQuestItemsCount(EinhasadChurchMark3) == 1 && npcId == 20404)
		{
			if (st.getQuestItemsCount(HorribleSkull) < 10)
				st.giveItems(HorribleSkull, 1);
			else if (st.getQuestItemsCount(HorribleSkull) == 10)
				st.playSound("ItemSound.quest_middle");
		}
		return null;
	}

	public static void main(String[] args)
	{
		new Q402_PathToAHumanKnight(402, qn, "Path to a Human Knight");
	}

}	